package com.itheima.springmvc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.itheima.springmvc.mapper.Pojovo;
import com.itheima.springmvc.pojo.Items;
import com.itheima.springmvc.service.ItemService;

import sun.awt.ModalExclude;

/** 
* @author 上午茶
* @version 创建时间：2018年6月28日 下午12:45:18 
* 类说明 
*/
@Controller
public class ItemsController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/itemsList")
	public ModelAndView getItmesList(){
		List<Items> list = itemService.getItems();
		
		ModelAndView view = new ModelAndView();
		
		view.addObject("itemList", list);
		
		view.setViewName("itemList");
		
		return view;
	}
	
	@RequestMapping("/itemEdit/{id}")
	public String getItemEdit(Integer id,Model model){

		Items items = itemService.getItemEdit(id);
		
		model.addAttribute("item",items);
		
		return "editItem";
	}
	
	@RequestMapping("/updateitem")
	public String getupdateitem(Items items,MultipartFile picedtureFile,HttpServletRequest request) throws IllegalStateException, IOException{
		
		String filename = UUID.randomUUID().toString();
		System.out.println("aaaa");
		String startsname = picedtureFile.getOriginalFilename();
		String endname = startsname.substring(startsname.lastIndexOf("."));
		
		String path = request.getSession().getServletContext().getRealPath("/upload");
		
		//File destFile = new File(path+File.separator+"upload"+File.separator+ filename+endname);
		
		picedtureFile.transferTo(new File(path+File.separator+ filename+endname));
				
		//把文件名保存到数据库
		items.setPic(filename + endname);
		
		boolean items2 = itemService.updateItems(items);
		System.out.println(items.getCreatetime());
		
		if (items2) {
			return "success";
		}
		
		return "erroe";
	}
	
	@RequestMapping("/queryitem")
	public String getItmesList2(Pojovo pojovo,String[] ids){
		System.out.println(pojovo.getList().get(0).getName());
		System.out.println(pojovo.getList().get(1).getName());
		System.out.println(ids[0]);
		System.out.println(ids[1]);
		return "success";
	}
	
	
	@RequestMapping("/jsontest")
	@ResponseBody
	public Items jsontest(@RequestBody Items items){

		return items;
	}
	
}
